package org.example.script;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.example.HttpSend;
import org.example.Main;
import org.example.leetcode.common.domain.SysUser;
import org.example.leetcode.common.pojo.GlobalResponse;
import org.example.leetcode.core.dao.SysUserMapper;
import org.example.leetcode.core.dao.TQuestionMapper;
import org.example.leetcode.core.domain.TQuestion;
import org.example.leetcode.core.dto.SysUserTokenDto;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class GetQuestionInfoScript {
    public static void main(String[] args) throws Exception {
        SysUserMapper sysUserMapper= MyBatisUtils.getMybatisMapper(SysUserMapper.class);
        TQuestionMapper tQuestionMapper = MyBatisUtils.getMybatisMapper(TQuestionMapper.class);
        List<TQuestion> tQuestions = tQuestionMapper.selectAll();
        List<SysUser> sysUsers = sysUserMapper.selectAll();
        Random random=new Random();
        for(int i=0;i<500;i++){
            int index=random.nextInt(sysUsers.size());
            SysUser sysUser=sysUsers.get(index);
            String tokenUrl="http://127.0.0.1:8000/core/sys-user/login/passwordLogin/"+sysUser.getPhone()+"/"+sysUser.getPassword();
            String s = HttpSend.sendHttp(tokenUrl, null,"GET",null);
            GlobalResponse globalResponse = JSONObject.parseObject(s, GlobalResponse.class);
            JSONObject object=(JSONObject)globalResponse.getData();
            String token = (String) object.get("token");
            System.out.println(token);
            Map<String,String> map=new HashMap<>();
            map.put("Authorization",token);
            for(int j=0;j<500;j++){
                TQuestion tQuestion = tQuestions.get(random.nextInt(tQuestions.size()));
                String getInfoUrl="http://127.0.0.1:8000/core/t-question/info/"+tQuestion.getTitleSlug();
                HttpSend.sendHttp(getInfoUrl,null,"GET",map);
                System.out.println("第"+i+"人"+"第"+j+"次");
            }

        }
    }

}
